import { Controller } from 'egg';

export default class HomeController extends Controller {
  public async index() {
    await this.ctx.render('index.ejs');
  }
  public async getBarCode() {
    const { ctx } = this;
    const code = this.ctx.query.code;
    if (!code) {
      ctx.body = { msg: '求传入条形码!' };
      ctx.status = 400;
    } else {
      const barCode = await this.app.mysql.get('BarCode', { code });
      console.log(barCode);
      if (barCode) {
        ctx.status = 422;
        ctx.body = { msg: `${code}已经被录入` };
      } else {
        ctx.status = 200;
        await this.app.mysql.insert('BarCode', { code });
        ctx.body = { msg: '该二维码是新的' };
      }
    }
  }
}
